﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class NewsTypeController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();
        // GET: NewsType
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Search(int page, int limit, string TypeName)
        {
 
            //db.Configuration.LazyLoadingEnabled = false;


            var q = db.NewsType.Where(p => true);
            if (TypeName != "" && TypeName != null)
            {
                q = q.Where(p => p.TypeName.Contains(TypeName));
            }

            var count = q.Count();//得到总统计数
            //根据时间排序，并实现分页
            var list = q.OrderByDescending(x => x.id).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
            {
                id = x.id,
                TypeName = x.TypeName,

            });
            return Json(new { code = 0, msg = "", count = count, data = list });
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(NewsType newsType)
        {
 
            {
                NewsType n = new NewsType();//创建一个新闻对象
                n.TypeName = newsType.TypeName;//新闻标题

                db.NewsType.Add(n);//添加新闻
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { suc = true, msg = "添加成功" });
                }
                else
                {
                    return Json(new { suc = true, msg = "添加失败" });

                }
            }
        }

        public ActionResult Edit(int id)
        {
 
            {
                var info = db.NewsType.SingleOrDefault(p => p.id == id);
                ViewBag.A = info;
            }
            return View();
        }
        public ActionResult Update(NewsType newsType)
        {
 
            {
                var obj = db.NewsType.SingleOrDefault(x => x.id == newsType.id);

                obj.TypeName = newsType.TypeName;

                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "编辑成功" });
                }
                else
                {
                    return Json(new { code = 1, msg = "编辑失败" });
                }
            }

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delect(int id)
        {
 
            var obj = db.NewsType.FirstOrDefault(x => x.id == id);
            if (obj != null)
            {
                db.NewsType.Remove(obj);//删除该新闻
            }
            int r = db.SaveChanges();
            if (r > 0)
            {
                return Json(new { scu = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { scu = true, msg = "删除失败" });

            }
        }
    }
}